import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Calendar;
import java.util.Date;

import javax.microedition.rms.RecordStore;

public class MyIO {
	
	public static final String REC_SCORE = "lines_score";
	public static final String REC_SHAPE = "lines_shape";
	
	public static GameShape loadGameShape() {
		try {
			RecordStore rs = RecordStore.openRecordStore(REC_SHAPE, false);
			if (rs.getNumRecords() < 3) return null;
			
			byte[] a = rs.getRecord(1);
			byte[][] aa = new byte[9][9];
			int k = 0;
			for (int i = 0; i < 9; i++)
				for (int j = 0; j < 9; j++)
					aa[i][j] = a[k++];
			
			ByteArrayInputStream byteStream =
					new ByteArrayInputStream(rs.getRecord(2));
			int score = new DataInputStream(byteStream).readInt();
			byteStream.close();
			
			byte type = rs.getRecord(3)[0];
			rs.closeRecordStore();
			return new GameShape(aa, score, type);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void saveGameShape(GameShape gs) {
		try {
			RecordStore.deleteRecordStore(REC_SHAPE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			RecordStore rs = RecordStore.openRecordStore(REC_SHAPE, true);
			
			byte[] a = new byte[81];
			int k = 0;
			for (int i = 0; i < 9; i++)
				for (int j = 0; j < 9; j++)
					a[k++] = gs.a[i][j];
			rs.addRecord(a, 0, a.length);
			
			ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
			DataOutputStream dataStream = new DataOutputStream(byteStream);
			dataStream.writeInt(gs.score);
			byte[] b = byteStream.toByteArray();
			rs.addRecord(b, 0, b.length);
			dataStream.close();
			byteStream.close();
			
			a[0] = gs.type;
			rs.addRecord(a, 0, 1);
			
			rs.closeRecordStore();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static ScoreRecord[] loadHighScore() {
		try {
			RecordStore rs = RecordStore.openRecordStore(REC_SCORE, false);
			int number = rs.getNumRecords();
			ScoreRecord[] arr = new ScoreRecord[number];
			for (int i = 1; i <= number; i++) {
				ByteArrayInputStream byteStream =
					new ByteArrayInputStream(rs.getRecord(i));
				DataInputStream dataStream = new DataInputStream(byteStream);
				int score = dataStream.readInt();
				String date = dataStream.readUTF();
				arr[i - 1] = new ScoreRecord(date, score);
			}
			rs.closeRecordStore();
			return arr;
		} catch (Exception e) {
			e.printStackTrace();
			ScoreRecord[] scr = createDefault();
			writeHighScore(scr);
			return scr;
		}
	}
	
	public static void writeHighScore(ScoreRecord[] arr) {
		try {
			RecordStore.deleteRecordStore(REC_SCORE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			RecordStore rs = RecordStore.openRecordStore(REC_SCORE, true);
			for (int i = 0; i < arr.length; i++) {
				ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
				DataOutputStream dataStream = new DataOutputStream(byteStream);
				dataStream.writeInt(arr[i].score);
				dataStream.writeUTF(arr[i].date);
				byte[] data = byteStream.toByteArray();
				rs.addRecord(data, 0, data.length);
			}
			rs.closeRecordStore();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static ScoreRecord[] createDefault() {
		int n = 10;
		ScoreRecord[] scr = new ScoreRecord[n];
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		for (int i = 0; i < n; i++) {
			scr[i] = new ScoreRecord(convertToStr(c), 10);
		}
		return scr;
	}
	
	public static String convertToStr(Calendar c) {
		return c.get(Calendar.MONTH) + 1 + "/" + c.get(Calendar.DATE) + "/"
				+ c.get(Calendar.YEAR);
	}
	
}
